package com.soboot.gateway.config;

import com.soboot.gateway.handler.HystrixFallbackHandler;
import com.soboot.gateway.handler.SwaggerResourceHandler;
import com.soboot.gateway.handler.SwaggerSecurityHandler;
import com.soboot.gateway.handler.SwaggerUiHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;

/**
 * @Author:
 * @createTime: 2022年09月19日 17:56:38
 * @version:
 * @Description:
 * @Copyright:
 */
@Slf4j
@Component
@AllArgsConstructor
public class RouterFunctionConfiguration {

    private final HystrixFallbackHandler hystrixFallbackHandler;

    private final SwaggerResourceHandler swaggerResourceHandler;

    private final SwaggerUiHandler swaggerUiHandler;

    private final SwaggerSecurityHandler swaggerSecurityHandler;

    @Bean
    public RouterFunction routerFunction() {
        return RouterFunctions.route(
                        RequestPredicates.path("/fallback")
                                .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources")
                        .and(RequestPredicates.accept(MediaType.ALL)), swaggerResourceHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources/configuration/ui")
                        .and(RequestPredicates.accept(MediaType.ALL)), swaggerUiHandler)
                .andRoute(RequestPredicates.GET("/swagger-resources/configuration/security")
                        .and(RequestPredicates.accept(MediaType.ALL)), swaggerSecurityHandler);
    }
}
